import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:psi/utils/utils.dart';
import 'package:psi/utils/widget_util.dart';
import 'package:psi/widgets/text_widget.dart';

import '../generated/assets.dart';
import '../theme/app_theme.dart';
import 'gesture.dart';

class BackBtn extends StatelessWidget {
  final Color? color;
  final GestureTapCallback? onTap;
  final String? imgAssets;
  final double? size;

  const BackBtn({Key? key, this.color, this.onTap, this.imgAssets, this.size}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return UnconstrainedBox(
      child: ClickWidget(
        onTap: onTap ??
            () {
              Get.back();
            },
        child: WidgetUtil.assetImage(imgAssets ?? R.iconBack, size: size ?? 28.w, color: color, boxFit: BoxFit.cover),
      ),
    );
  }
}

class PageTitle extends StatelessWidget {
  final String title;
  final TextStyle? style;

  const PageTitle(this.title, {Key? key, this.style}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return TextWidget(
      title,
      style: style,
    );
  }
}

class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
  final String? title;
  final String? backImgAssets;
  final double? elevation;
  final bool showBack;
  final Widget? titleWidget;
  final Widget? leading;
  final GestureTapCallback? onBackPressed;
  final double? fontSize;
  final List<Widget>? actions;

  const MyAppBar({
    Key? key,
    this.showBack = true,
    this.titleWidget,
    this.leading,
    this.onBackPressed,
    this.fontSize,
    this.title,
    this.backImgAssets,
    this.actions,
    this.elevation = 0,
  }) : super(key: key);


  @override
  Widget build(BuildContext context) {
    return AppBar(
      elevation: elevation,
      leading: leading ?? (showBack ? BackBtn(imgAssets: backImgAssets, color: Colors.white, onTap: onBackPressed) : null),
      automaticallyImplyLeading: false,
      centerTitle: true,
      title: title != null
          ? PageTitle(
              title!,
              style: pageTitleTextStyle,
            )
          : titleWidget,
      // actions: actions,
      actions: actions,
      // backgroundColor: appBarMode == AppBarMode.light ? Colors.white : const Color(0xFF1C2137),
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(54.w);
}

class EmptyAppBar extends StatelessWidget implements PreferredSizeWidget {
  const EmptyAppBar({super.key});

  @override
  Widget build(BuildContext context) {
    return const SizedBox.shrink();
  }

  @override
  Size get preferredSize => const Size.fromHeight(0);
}
